package com.dingqb.myapplication.paint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Create by dingqb
 * On 2019-08-20
 * Description: quad 贝济埃曲线
 */
public class MoveFromFingerbejizer extends View {
    public MoveFromFingerbejizer(Context context) {
        super(context);
    }

    public MoveFromFingerbejizer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MoveFromFingerbejizer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private Path mPath = new Path();

    public static final String TAG = "dingqb";

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.e(TAG, "dispatchTouchEvent: " + event.getAction());
        return super.dispatchTouchEvent(event);
    }

    private float mPrex;
    private float mPrey;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e(TAG, "onTouchEvent: " + event.getAction());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                mPath.moveTo(event.getX(), event.getY());
                mPrex = event.getX();
                mPrey = event.getY();
                return true;
            }
            case MotionEvent.ACTION_MOVE:
                float endX = (mPrex + event.getX()) / 2;
                float endY = (mPrey + event.getY()) / 2;
                // 二级贝吉哀曲线
                mPath.quadTo(mPrex, mPrey, endX, endY);
                mPrex = event.getX();
                mPrey = event.getY();
                //This method can be invoked from outside of the UI thread
                // only when this View is attached to a window.</p>
                postInvalidate();
                //This must be called from a UI thread
//                invalidate();
                break;
            case MotionEvent.ACTION_UP:

                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.e(TAG, "onDraw: density==" + canvas.getDensity());
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStrokeWidth(10);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        canvas.drawColor(Color.GRAY);
        canvas.drawPath(mPath, paint);

    }

    public void reset() {
        mPath.reset();
        invalidate();
    }
}
